package org.cdc.divination.frame.data;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class DataPool {
    private static Map<String,PoolValue> publicDataPool = new HashMap<>();
    public static void addPublicData(String name,Object value,boolean publicStat){
        publicDataPool.put(name,new PoolValue(value,publicStat));
    }
    public static void removePublicData(String name){
        publicDataPool.remove(name);
    }
    public static Object getPublicData(String name){
        return publicDataPool.get(name).getValue();
    }

    public static Map<String, PoolValue> getPublicDataPool() {
        return publicDataPool;
    }

    public Map<String, Object> getLocalDataPool() {
        return localDataPool;
    }

    private Map<String,Object> localDataPool;

    public Object getLocalData(String name){
        return localDataPool.get(name);
    }
    public void removeLocalData(String name){
        localDataPool.remove(name);
    }
    public void addLocalData(String name,Object value){
        localDataPool.put(name, value);
    }

    /**
     * 得到符合条件的数据
     * @param varName 变量名称
     * @param varClass 变量类型
     * @return 如果找到了符合条件的变量 那么返回该变量,否则 返回null
     */
    public Object getDataByCondition(String varName,String varClass){
        //首先寻找是否存在于其中之一
        if (publicDataPool.containsKey(varName)){
            PoolValue obj = publicDataPool.get(varName);
            if (obj.getClass().equals(varClass)){
                return (obj.isPublicStat())?obj:null;
            }
        } else if (localDataPool.containsKey(varName)){
            Object obj = localDataPool.get(varName);
            if (obj.getClass().equals(varClass)){
                return obj;
            }
        }
        return null;
    }
    /**
     * 得到符合条件的数据
     * @param varName 变量名称
     * @param varClass 变量类型
     * @return 如果找到了符合条件的变量 那么返回该变量,否则 返回null
     */
    public static Object getPublicDataByCondition(String varName,String varClass){
        //首先寻找是否存在于其中之一
        if (publicDataPool.containsKey(varName)){
            PoolValue obj = publicDataPool.get(varName);
            if (obj.getClass().equals(varClass)){
                return (obj.isPublicStat())?obj:null;
            }
        }
        return null;
    }
}
